package com.ea.config;

import com.ea.common.enums.OperationResultCode;
import com.ea.common.enums.SysCfgKeyEnums;
import com.ea.common.exception.ServiceException;
import com.ea.domain.entity.SysBaseCfg;
import com.ea.mapper.SysBaseCfgMapper;
import com.ea.mapper.SysBaseDictionaryMapper;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author NAME-L
 * @Description TODO
 * @className eleaInitDataonfig
 * @date 2023-01-27 17:14:08
 */
@Component
public class initDataConfig implements ApplicationRunner {

    private static final Logger logger = LoggerFactory.getLogger(initDataConfig.class);

    @Autowired
    private SysBaseCfgMapper cfgMapper;

    @Autowired
    private SysBaseDictionaryMapper dictionaryMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void run(ApplicationArguments args) throws Exception {

        try {
            initSysConfig();
        } catch (Exception e) {
            logger.error("初始化系统配置失败！" , e);
        }

        try {
            initSysDictionary();
        } catch (Exception e) {
          logger.error("初始化系统字典失败！" , e);
        }
    }



    public void initSysConfig(){
        logger.info("初始化系统配置开始 ====>");
        long start = System.currentTimeMillis();
        try {
            List<String> cfgKeyList = SysCfgKeyEnums.initCfgKeyList();

            if (CollectionUtils.isNotEmpty(cfgKeyList)) {
                List<SysBaseCfg> sysBaseCfgList = cfgMapper.getSysBaseCfgList(cfgKeyList);
                for (SysBaseCfg cfg : sysBaseCfgList) {
                    cfgKeyList.removeIf(key ->key.equals(cfg.getCfgKey()));
                }
            }
            List<SysBaseCfg> initSysCfgList = new ArrayList<>();
            for (String cfgKey : cfgKeyList) {
                SysBaseCfg initSysCfg = new SysBaseCfg();
                initSysCfg.setCfgKey(cfgKey);
                initSysCfg.setCfgValue(SysCfgKeyEnums.getDefaultValue(cfgKey));
                initSysCfg.setCfgDesc(SysCfgKeyEnums.getCfgDesc(cfgKey));
                initSysCfg.setCfgType(SysCfgKeyEnums.getCfgType(cfgKey));
                initSysCfgList.add(initSysCfg);
            }
            if (CollectionUtils.isNotEmpty(initSysCfgList)) {
                cfgMapper.insertBatch(initSysCfgList);
            }
        } catch (Exception e) {
            throw new ServiceException(OperationResultCode.SYSTEM_ERROR);

        }
        long end = System.currentTimeMillis();
        logger.info("初始化系统配置结束 ====> 耗时：{}ms" , end - start);
    }


    public void initSysDictionary() {

    }
}
